﻿<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="XProject.DataVisit.Model.Advertisement" %>
<%@ Import Namespace="XProject.DataVisit.Model" %>
<%@ Import Namespace="XProject.DataVisit.BLL" %>
<%@ Import Namespace="XProject.DataVisit.Factory" %>
<%
    string gname = Request["Ad_Name"];
    string Remark = Request["Ad_Remark"];
    string Period = Request["Ad_Period"];
    string FileName = Request.Files["Ad_Upload"].FileName;
    Stream filec = Request.Files["Ad_Upload"].InputStream;
    int FileLen = Request.Files["Ad_Upload"].ContentLength;
    byte[] fileData = new byte[FileLen];
    filec.Read(fileData, 0, FileLen);
    string filetype = Path.GetExtension(Request.Files["Ad_Upload"].FileName);
    //保存压缩文件
    string RarName=DateTime.Now.ToString("yyyyMMdd") + FileLen.ToString() + DateTime.Now.ToString("hhmmss") + DateTime.Now.Millisecond.ToString()  + filetype;
    string RarPath = Server.MapPath("../") + "/AdvertisementFile/Advertisements/";
    var savepath = RarPath + RarName;
    File.WriteAllBytes(savepath, fileData);
    //解压文件,并返回广告路径
    new XProject.DataVisit.BLL.Rar().unCompressRAR(RarPath, RarPath, RarName);
    string Adpath = "AdvertisementFile/Advertisements/" + Path.GetFileName(Request.Files["Ad_Upload"].FileName).Split('.')[0] + "/index.js";
    
    //重新打开JS文件，替换服务器路径
    string ht = File.ReadAllText(RarPath + Path.GetFileName(Request.Files["Ad_Upload"].FileName).Split('.')[0] + "/index.js");
    if (ht.IndexOf("#p")>0)
    { 
        ht=ht.Replace("#p",new Config().GetConfig());
        File.WriteAllText(RarPath + Path.GetFileName(Request.Files["Ad_Upload"].FileName).Split('.')[0] + "/index.js", ht);
    }
    //删除压缩包
    File.Delete(savepath);
    
    //保存进数据库
    Advertisement at = new Advertisement();
    at.Lastupdate = DateTime.Now;
    at.Creater = ((UserInfo)Session["UserInfo"]).PID;
    at.GName = gname;
    at.TransfState = 0;
    at.GRemark = Remark;
    at.GUrl = Adpath;
    at.FolderName = Path.GetFileName(Request.Files["Ad_Upload"].FileName).Split('.')[0];
    at.Period = Convert.ToInt32(Period);
    at.GType = "1";
    DaoFactory.AdvertisementDao.Save(at);
    
    Dictionary<string, object> data = new Dictionary<string, object>();
    data.Add("success", true);
    Response.Clear();
    Response.Write(Newtonsoft.Json.JavaScriptConvert.SerializeObject(data));
    Response.End();
 %>